import '../../style/sign.less';
import React, {Component} from 'react';
import ReactDOM from 'react-dom';
import {Popover} from 'antd';
import { WeaLocaleProvider } from 'ecCom';
import * as api from "../../api/signPlugin";
import Header from './Header';
import DataList from './DataList';
const { getLocaleLabelByCode, getLabel } = WeaLocaleProvider;

export default class HrmSingInfo extends Component{
    constructor(props){
        super(props);
        this.state = {
            date: null,
            i18nLoaded: false,
            visible:false,
            hidePopHandle: () => this.setState({visible: false})
        }
    }

    componentDidMount(){
        getLocaleLabelByCode('hrm').then(() => {
            this.setState({ i18nLoaded: true });
        });
        //检查签到签退状态
    }

    handleVisibleChange = visible => {
        this.setState({visible});
    };

    hide = () => this.setState({visible: false});

    setDate = date => {
        this.headerRef && this.headerRef.setDate(date);
    }

    render(){
        const content = <DataList ecId={`${this && this.props && this.props.ecId || ''}_DataList@0pj3sn`} {...this.state} hide={this.hide} setDate={this.setDate} userInfo={this.props.userInfo}/>;
        const title = <Header ecId={`${this && this.props && this.props.ecId || ''}_Header@dqabo8`} {...this.state} ref={dom => this.headerRef = dom}/>
        return (
            <Popover ecId={`${this && this.props && this.props.ecId || ''}_Popover@jbd4mb`} content={content} title={title} trigger="click" placement="bottomRight" overlayClassName={'singPop'}
                visible={this.state.visible} onVisibleChange={this.handleVisibleChange}
            >
                <div className='singBtn'>
                    <i className='icon-coms-New-schedule'/>
                    <span>{getLabel('15880',"考勤")}</span>
                </div>
            </Popover>
        )
    }
}

(
    () => {
        let isMobile = false;
        if ((navigator.userAgent.match(/(iPhone|iPod|Android|ios|iOS|iPad|Backerry|WebOS|Symbian|Windows Phone|Phone)/i))) {
            isMobile = true;
        }
        api.getButtonBaseInfo({isMobile}).then(data => {
            if(data.status === '1'){
                if(data.showbutton != '0'){
                    const userInfo = {
                        date: data.date,
                        groupname: data.groupname,
                        lastname: data.lastname,
                        shortname: data.shortname,
                        messagerurl: data.messagerurl,
                        showbutton: data.showbutton
                    };
                    const timer = setInterval(() => {
                        if($('#signPlugin').length > 0){
                            ReactDOM.render(<HrmSingInfo ecId={`${this && this.props && this.props.ecId || ''}_HrmSingInfo@t2pf12`} userInfo={userInfo}/>, document.getElementById('signPlugin'));
                            clearInterval(timer);
                        }
                    }, 500);
                }
            }else{
                message.error(data.message);
            }
        }, error => {});
    }
)();